# This is a sample Python script.
import pandas as pd
import fill_missing as fill
import category_var_code as code
import data_extract as de
# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
import numpy as np
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    # data = pd.read_csv(r'data_without_name_time.csv', index_col=0)
    # mutiple_field = ['status_second', 'inventory_type']
    # for field in mutiple_field:
    #     new_chart = data[['promotion_id', field]]
    #     sample_num = new_chart.shape[0]
    #     data = data.drop(columns=field)
    #     data.to_csv(r'.//data_without_name_time_multiple.csv')
    #     for index, row in new_chart.iterrows():
    #         value = row[field]
    #         promotion_id = row['promotion_id']
    #         if pd.isna(value):
    #             new_chart = new_chart._append({"promotion_id": promotion_id, field: np.nan},
    #                                                     ignore_index=True)
    #             continue
    #         value = eval(value)
    #         if isinstance(value, list):
    #             for value in value:
    #                 new_chart = new_chart._append({"promotion_id": promotion_id, field: value}, ignore_index=True)
    #         else:
    #             new_chart = new_chart._append({"promotion_id": promotion_id, field: value}, ignore_index=True)
    #     new_chart = new_chart.drop(axis=0, index=range(0, sample_num))
    #     new_chart.to_csv(r'.//new_chart_'+field)
    #     print(1)



    # data = pd.read_csv(r'.//data_without_name_time_multiple.csv', index_col=0)
    # for column, value in data.items():
    #     value_num = len(value.unique())
    #     if 2 <= value_num < 10:
    #         fill.fit_category_var(data, column)
    #     else:
    #         continue
    # data.to_csv(r'.//data_without_name_time_multiple_nan.csv')
    # print(1)
    data = pd.read_csv(r'.//ad.csv')
    data = de.remove_nan_column(data)
    dict_field = ['optimize_goal', 'delivery_range', 'delivery_setting', 'audience', 'native_setting']
    data = data.drop(columns=['track_url_setting', 'promotion_materials', 'file_list', 'material_ids'])
    data = de.dict_data_extract(data, dictionary_field=dict_field)
    data = data.drop(columns=['product_name', 'advertiser_name', 'name', 'project_create_time', 'project_modify_time', 'promotion_name', 'promotion_modify_time', 'promotion_create_time', 'end_time', 'schedule_time', 'start_time'])
    data = de.related_product_num_calculate(data)
    data = data.drop(columns='asset_ids')
    multiple_field = ['status_second', 'inventory_type']
    data = de.multiple_data_extract(data, multiple_field=multiple_field)
    data = data.drop(columns='platform') # only one value
    data = de.remove_nan_column(data)
    for column, value in data.items():
        value_num = len(value.unique())
        if 2 <= value_num < 10:
            fill.fit_category_var(data, column)
        else:
            continue
    # data = pd.read_csv(r'.//data_without_name_time_multiple_nan.csv', index_col=0)
    cnt_field = {'stat_cost': 0, 'click_cnt': 0, 'show_cnt': 0, 'convert_cnt': 0}
    data = data.drop(columns='project_id')
    data = data.fillna(0)
    # data.to_csv(r'.//data_without_name_time_multiple_nan.csv')

    # data = pd.read_csv(r'.//data_without_name_time_multiple_nan.csv', index_col=0)
    data = code.var_code(data)
    data = data.drop(columns='source')
    print(1)
    data.to_csv(r'.//data_without_name_time_multiple_nan_category.csv')
    output = data.loc[:, 'stat_cost':'convert_cnt']
    input = data.drop(columns=['stat_cost', 'click_cnt', 'show_cnt', 'convert_cnt'])
    output.to_csv(r'.//output.csv')
    input.to_csv(r'.//input.csv')

